package com.etc.controller;

import com.etc.config.LoadBalancer;
import com.etc.config.MyLB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sun.swing.StringUIClientPropertyKey;

import java.util.List;

/**
 * @Author kalista
 * @Description
 * @Date 2021/3/19  18:50
 **/
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    // 服务发现
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    LoadBalancer loadBalancer;

    @GetMapping("/index")
    public String index() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        // 没有负载均衡
        ServiceInstance instances1 = loadBalancer.instances(instances);

        String forEntity =  restTemplate.getForObject(instances1.getUri()+"/payment/index", String.class);

        return forEntity;
    }
}
